class AdminsController < ApplicationController
  filter_access_to :all
  before_filter :require_user  

  #
  #
  def show
    @user_search = User.search(params[:search])
    store_location

    if params[:all_admins]
      #@user_search = User.all_admin
    end

    if params[:all_mods]

    end

    if params[:search] != nil
      @users = @user_search.all
    end
  end

  #
  #
  def update
   @user = User.find(params[:user][:id])

    #Checks if the user is active or inactive
    if params[:user][:active] == nil
      @user.active = false
    else
      @user.active = true
    end

    #Resets users roles
    if params[:user][:role_ids] != nil
      @user.roles.clear
      params[:user][:role_ids].each do |role|
        new_role = Role.find(role)
        @user.roles << new_role
      end
    end

    #Checks if the user needs to be banned
    if params[:user][:banned] == nil
      @user.banned = false
    else
      @user.banned = true
    end

    #Saves the changes
    if @user.save
      flash[:notice] = "User successfully updated."
    else
      flash[:notice] = "There was an error updating that user."
    end

    redirect_to admin_path

  end

  #
  #
  def destroy
    @user = User.find(params[:id])

    User.transaction do
      #Delete all associations
      @user.forum_threads.each do |thread|
        thread.user_id = 7
        thread.save
      end
      @user.posts.each do |post|
        post.user_id = 7
        post.save
      end
      @user.assignments.each do |assignment|
        assignment.destroy
      end

      @user.destroy
      flash[:notice] = "User Successfully deleted."
    end
    redirect_to admin_path

  end

  
end
